import 'package:elephant_fish/http/core/hi_net.dart';
import 'package:elephant_fish/http/request/agree_applicaiton_request.dart';
import 'package:elephant_fish/http/request/base_request.dart';
import 'package:elephant_fish/model/user_info_entity.dart';
import 'package:elephant_fish/util/TxUtils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:elephant_fish/util/Alert.dart';
import 'package:shared_preferences/shared_preferences.dart';

class FamilyApplication extends StatefulWidget {
  final UserInfoEntity userInfoEntity;
  const FamilyApplication({Key key, this.userInfoEntity}) : super(key: key);

  @override
  _FamilyApplicationState createState() => _FamilyApplicationState();
}

class _FamilyApplicationState extends State<FamilyApplication> {
  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        SizedBox(width: 18,),
        InkWell(
          child: Container(
            width: 40,
            height: 40,
            child: CircleAvatar(
              radius: 25,
              backgroundImage: NetworkImage('http://119.28.131.94:82' + widget.userInfoEntity.avatar),
            ),
          ),
          onTap: (){
            showJoinDialog();
          },
        ),
        SizedBox(width: 9.5,),
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(widget.userInfoEntity.nickname,style: TextStyle(color: Color(0xffffffff),fontSize: 15),),
            SizedBox(height: 6,),
            Text('ID:' + widget.userInfoEntity.idNumber,style: TextStyle(color: Color(0x70ffffff),fontSize: 13),),
          ],
        ),
        SizedBox(width: 90,),
        InkWell(
          child: Container(
            width: 66,
            height: 28,
            decoration: BoxDecoration(
              color: Color(0xffe62d6c),
              borderRadius: BorderRadius.all(Radius.circular(50)),
            ),
            child: Center(
              child: Text(
                '同意',style: TextStyle(color: Color(0xfff3f3f3),fontSize: 14),
              ),
            ),
          ),
          onTap: (){
            showJoinDialog();
          },
        ),
        SizedBox(width: 10,),
        InkWell(
          child: Container(
            width: 66,
            height: 28,
            decoration: BoxDecoration(
              border: Border.all(color: Color(0xffe4e4e4),width: 1)
            ),
            child: Center(
              child: Text(
                '拒绝',style: TextStyle(color: Color(0xfff3f3f3),fontSize: 14),
              ),
            ),
          ),
        ),

      ],
    );
  }

  Future<void> showJoinDialog() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String appToken = prefs.getString('app-token');
    Alert.showCustomDialog(
        context: context,
        title: '同意申请',
        message: '您确定同意'+ widget.userInfoEntity.nickname + '(ID:'+ widget.userInfoEntity.idNumber + ')' +'的申请吗？',
        certainPressed: () async {
          BaseRequest request = AgreeApplicationRequest();
          request.add('id', widget.userInfoEntity.id).addHeader('usertoken', appToken);
          var result = await HiNet.getInstance().fire(request);
          if (result['code'] == 0) {
            TxUtils.showToast('操作成功', context);

          } else {
            TxUtils.showToast(result['message'], context);
          }
        });
  }


}

